package com.cy.store.controller;

import com.cy.store.entity.Address;
import com.cy.store.entity.Cart;
import com.cy.store.service.IAddressService;
import com.cy.store.service.ICartService;
import com.cy.store.util.JsonResults;
import com.cy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 购物车控制类
 */
@RestController //表示该控制类都以Json格式进行响应
//@Controller
@RequestMapping("cart")
public class CartController extends BaseController {
    @Autowired
    private ICartService cartService;

    @RequestMapping("add_to_cart")
    public JsonResults<Void> addCart(HttpSession httpSession, Integer pid,Integer amount){
        cartService.addCart(getuidFromSession(httpSession),amount,pid,getUsernameFromSession(httpSession));
        return new JsonResults<>(OK);
    }

    @RequestMapping({"","/"})
    public JsonResults<List<CartVO>> getCartProductByUid(HttpSession httpSession){
        List<CartVO> data = cartService.getCartProductByUid(getuidFromSession(httpSession));
        return new JsonResults<>(OK,data);
    }

    @RequestMapping("{cid}/num/add")
    public JsonResults<Integer> addNum(@PathVariable("cid") Integer cid,HttpSession httpSession){
        Integer data= cartService.addNum(getuidFromSession(httpSession),cid,getUsernameFromSession(httpSession));
        return new JsonResults<>(OK,data);
    }

    @RequestMapping("{cid}/num/reduce")
    public JsonResults<Integer> reduceNum(@PathVariable("cid") Integer cid,HttpSession httpSession){
        Integer data= cartService.reduceNum(getuidFromSession(httpSession),cid,getUsernameFromSession(httpSession));
        return new JsonResults<>(OK,data);
    }

    @RequestMapping("list")
    public JsonResults<List<CartVO>> getCartProductByCid(HttpSession httpSession,Integer[] cid){
        List<CartVO> data = cartService.getCartProductByCid(getuidFromSession(httpSession),cid);
        return new JsonResults<>(OK,data);
    }
}
